package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.zhaoshang.ZsTopOfRank;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_ZS_PROJECT_TOP_OF_RANK_FROM_CROSS, desc = "跨服同步榜首")
public class ZsWeekTopOfRankCmdFromCross implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsWeekRankTopRespMsgFromCross crossRespMsg = ZhaoShangProto.ZsWeekRankTopRespMsgFromCross.parseFrom(packet.getBytes());
        List<ZsTopOfRank> list = new ArrayList<>();
        int updateTime = DateHelper.getCurrentSecond();
        for(RankProto.TopRankTemp topRankTemp : crossRespMsg.getRankTopListList()){
            ZsTopOfRank userRank = new ZsTopOfRank();
            if(!StringUtils.isNullOrEmpty(topRankTemp.getRankTemp().getValue())) {
                userRank.setValue(Long.valueOf(topRankTemp.getRankTemp().getValue()));
            }
            userRank.setUserId(topRankTemp.getRankTemp().getUserId());
            userRank.setParam(topRankTemp.getRankTemp().getParam());
            userRank.setUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(topRankTemp.getRankTemp().getPlayerBaseData()));
            userRank.setBigRankType(eBigRankType.Normal.getValue());
            userRank.setRankType(topRankTemp.getType());
            userRank.setWeekId(crossRespMsg.getWeekId());
            userRank.setLastUpdateTime(updateTime);
            list.add(userRank);
        }
        ZsMgr.syncPreWeekRankTopFromCross(crossRespMsg.getWeekId(), list);
    }
}
